Skip to content

[calculator] 조경현 미션 제출합니다.#2

Open
khcho96 wants to merge 10 commits intowoowa-precourse-study:mainfrom
khcho96:khcho96
Open

[calculator] 조경현 미션 제출합니다.#2
khcho96 wants to merge 10 commits intowoowa-precourse-study:mainfrom
khcho96:khcho96

Conversation

@khcho96
Copy link

@khcho96 khcho96 commented Sep 19, 2025

java-calculator-precourse

구현 기능 목록

입력 문자열 처리

  • 빈 문자열인 경우

    • null 또는 빈 문자열("") 또는 1개 이상의 공백 입력 시 0을 반환
  • 커스텀 구분자를 지정한 경우

    • 커스텀 구분자가 //로 시작해 \n가 포함되는지 확인
    • 입력 문자열에서 커스텀 구분자를 추출
      • 1개 이상의 구분자이며, 문자와 숫자 모두 가능하다.
    • 추출한 커스텀 구분자를 기본 구분자 목록에 추가
    • 커스텀 구분자 지정 패턴(\n으로 끝나는 문자열) 이후의 문자열 추출
  • 공통

    • 구분자로 입력받은 문자열을 분리한다.
      • 계산기에서 모든 예외를 처리 하므로 일단 구분자로 모든 문자열을 분리한다.

계산기

  • 분리한 문자열을 양수 값으로 변환해 더해준다.
  • 양수 값 이외의 값이 배열에 들어있는 경우 예외 발생
  • 합계가 정수 최댓값을 초과하는 경우 예외 발생
  • 추출한 숫자의 합 반환

리펙토링

  • CustomController 클래스를 내부클래스로 변경, static 메서드를 인스턴스 메서드로 변경
  • 예외 발생 시 IllegalArgumentException(e)를 던져 stacktrace에 에러메시지 출력
  • 변수 이름 변경 VALID_CUSTOM_INPUT -> CUSTOM_DELIMITER_PATTERN

private final String delimiter;

public Delimiter() {
delimiter = ",;";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

세미콜론(;) 오타인 것 같습니다! 시험 때 마주치고 싶지 않은 매우 무시무시한 버그네요

이 오타를 보면서 저도 남일 같지 않아서.. 어떻게 하면 시험 당일에 이러한 버그를 금방 찾아낼 수 있을까 고민을 해보았습니다. 그래서 기본/엣지 케이스 입력값에 대한 짧은 테스트 코드 5개 정도는 ApplicationTest에 직접 추가하는 것이 좋을 것 같습니다. 특히, 해당 오타는 문제에서 준 대표 예시인 "1,2:3" → 6 만으로도 충분히 발견할 수 있기때문에 적어도 대표 예시는 test 코드로 만들어보는 연습을 해보는 것도 좋을 것 같습니다!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아니면 문제에서 제시된 기본 구분자 ,와 :를 코드에 바로 상수로 선언함으로써 오타가 발생할 가능성?을 줄일 수도 있을 것 같습니다.
private static final String DEFAULT_DELIMITER = ",:";

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

으악 끔찍한 버그가 있었네요
예외 상황을 처리하느라 기본 케이스는 오히려 간과한 결과인 듯 하네요..ㅎㅎ
테스트 코드는 작성을 하는 건지 몰랐는데 말씀해주신 덕분에 해볼 수 있을 것 같아요!
감사합니다 ㅎㅎ

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

세민님 코드 보면서 필요하다면 상수 처리하는 것이 효율적이라는 생각이 많이 들어요 ㅎㅎ

Copy link
Member

@chltmdgh522 chltmdgh522 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

확인 부탁드려요!

return result + number;
}

private class CustomController {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이건 따로 클래스 만들어서 해주면 좋을거 같아요!

public static void main(String[] args) {
// TODO: 프로그램 구현
System.out.println("덧셈할 문자열을 입력헤주세요.");
String input = Console.readLine();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input도 따로 인스턴스로 빼서 만들어주시면 좋을거 같아요!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

output도 동일!!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체적으로 구조를 나눠서 하시면 좋을거 같습니다! 우테코 프리코스 합격비법이 출력 정답도 중요하지만 코드 구조를 엄청 세심하게 평가한다고 해서 되도록 이면 OOP 특징을 잘활용해서 조립식으로 쪼개야 될거 같네요!! 고생하셨어요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

프로그램을 패키지별로 나누는 방법을 배우게 되는 계기가 되었네요 ㅎㅎ

private class CustomController {
private static final String CUSTOM_DELIMITER_PATTERN = "^//(.*)\\\\n(.*)";
private static final int CUSTOM_DELIMITER_GROUP = 1;
private static final int CUSTOM_PURE_INPUT_GROUP = 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그룹이 두 개로만 나뉘어서 customDelimiter=group(1) / customPureInput=group(2) 처럼 변수로 구분해도 될 것 같은데,
이 부분은 상수화 하신 이유가 궁금합니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

극한으로 하드코딩을 피하고자 이렇게 구현했습니다!
이번에 코드를 짜면서 과연 어디까지가 하드코딩인지 궁금해졌습니다ㅎㅎ
지영님은 어느정도까지가 하드코딩이라고 생각하시나요?


public Delimiter(String customDelimiter) {
this.delimiter = ",:" + customDelimiter;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자 오버로딩을 이용해서, 객체를 다르게 생성할 수도 있군요. 사용자 지정 구분자가 있는지 파악하는 로직 대신 생성자 오버로딩을 사용하면, 코드도 간결해져서 좋은 것 같습니다!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메타문자들은 Pattern.quote()로 감싸주면 좋을 것 같습니다!

@khcho96 khcho96 reopened this Oct 6, 2025
@khcho96
Copy link
Author

khcho96 commented Oct 6, 2025

java-calculator-precourse

구현 기능 목록

입력 문자열 처리

  • 빈 문자열인 경우

    • null 또는 빈 문자열("") 또는 1개 이상의 공백 입력 시 0을 반환
  • 커스텀 구분자를 지정한 경우

    • 커스텀 구분자가 //로 시작해 \n가 포함되는지 확인
    • 입력 문자열에서 커스텀 구분자를 추출
      • 1개 이상의 구분자이며, 문자와 숫자 모두 가능하다.
    • 추출한 커스텀 구분자를 기본 구분자 목록에 추가
    • 커스텀 구분자 지정 패턴(\n으로 끝나는 문자열) 이후의 문자열 추출
  • 공통

    • 구분자로 입력받은 문자열을 분리한다.
      • 계산기에서 모든 예외를 처리 하므로 일단 구분자로 모든 문자열을 분리한다.

계산기

  • 분리한 문자열을 양수 값으로 변환해 더해준다.
  • 양수 값 이외의 값이 배열에 들어있는 경우 예외 발생
  • 합계가 정수 최댓값을 초과하는 경우 예외 발생
  • 추출한 숫자의 합 반환

리펙토링

  • CustomController 클래스를 내부클래스로 변경, static 메서드를 인스턴스 메서드로 변경
  • 예외 발생 시 IllegalArgumentException(e)를 던져 stacktrace에 에러메시지 출력
  • 변수 이름 변경 VALID_CUSTOM_INPUT -> CUSTOM_DELIMITER_PATTERN

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants